package com.cashmama.market.api.domain.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cashmama.market.api.domain.vo.RequestLocalInfo;
import com.cashmama.market.api.domain.vo.order.repayment.RepaymentChannelConfig;
import com.cashmama.market.api.infras.config.ApolloConfigUtil;
import com.cashmama.market.api.infras.utils.VersionUtils;
import com.fqgj.common.api.Response;
import com.fqgj.exception.common.ApiErrorException;
import com.qianli.cashmama.pay.client.domain.query.request.ChannelQueryRequest;
import com.qianli.cashmama.pay.client.domain.query.request.PaymentQueryRequest;
import com.qianli.cashmama.pay.client.domain.query.response.ChannelQueryResponse;
import com.qianli.cashmama.pay.client.domain.query.response.PaymentQueryResponse;
import com.qianli.cashmama.pay.client.enums.PaymentChannelEnum;
import com.qianli.cashmama.pay.client.service.QueryService;
import com.qianli.cashmama.trade.client.trade.TradeQueryService;
import com.qianli.cashmama.trade.client.trade.response.TradeInfoResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @program: market-api
 * @description 还款渠道配置
 * @author: frank
 * @create: 2019-12-25 21:22
 **/
@Service
public class RepaymentService {
    private static final Logger LOGGER = LoggerFactory.getLogger(RepaymentService.class);
    @Autowired
    private TradeQueryService tradeQueryService;
    @Autowired
    private QueryService queryService;
    @Autowired
    private ApolloConfigUtil apolloConfigUtil;

    public List<RepaymentChannelConfig> getRepaymentChannelConfigList(String bizNo) {
        Response<TradeInfoResponse> tradeInfoResponse = tradeQueryService.queryByTradeNo(bizNo);
        ChannelQueryRequest channelQueryRequest = new ChannelQueryRequest();
        channelQueryRequest.setProductCategoryCode(tradeInfoResponse.getData().getProductCategory());
        Response<List<ChannelQueryResponse>> listResponse = queryService.repaymentChannelQuery(channelQueryRequest);
        List<ChannelQueryResponse> channelQueryResponseList = listResponse.getData();
        List<RepaymentChannelConfig> repaymentChannelConfigList = new ArrayList<>();
        Integer paymentChannel = null;
        if (apolloConfigUtil.razorPayLimit().contains(tradeInfoResponse.getData().getProductCategory())) {
            PaymentQueryRequest paymentQueryRequest = new PaymentQueryRequest();
            paymentQueryRequest.setProductCategoryCode(tradeInfoResponse.getData().getProductCategory());
            paymentQueryRequest.setBizNo(bizNo);
            Response<PaymentQueryResponse> paymentQueryResponseResponse = queryService.paymentQuery(paymentQueryRequest);
            try {
                paymentChannel = paymentQueryResponseResponse.getData().getPaymentChannel();
            } catch (NullPointerException e) {
            }
        }
        for (ChannelQueryResponse channelQueryResponse : channelQueryResponseList) {
            //对于razorpay 放款的用户只能使用razorpay还款
            boolean useRazorpay = Objects.equals(paymentChannel, PaymentChannelEnum.RAZORPAY.getCode()) && Objects.equals(channelQueryResponse.getChannelCode(), PaymentChannelEnum.RAZORPAY.getCode());
            //不是razorpay放款的用户不能使用razorpay
            boolean noRazorpay = !Objects.equals(paymentChannel, PaymentChannelEnum.RAZORPAY.getCode()) && !Objects.equals(channelQueryResponse.getChannelCode(), PaymentChannelEnum.RAZORPAY.getCode());
            if (paymentChannel == null || useRazorpay || noRazorpay) {
                LOGGER.info("== channelQueryResponse:{} ==", JSONObject.toJSONString(channelQueryResponse));
                RepaymentChannelConfig repaymentChannelConfig = new RepaymentChannelConfig();
                repaymentChannelConfig.setId(Integer.parseInt(channelQueryResponse.getIdInApp()));
                repaymentChannelConfig.setName(channelQueryResponse.getNameInApp());
                repaymentChannelConfig.setIcon(channelQueryResponse.getIcon());
                repaymentChannelConfig.setChannelServiceFee(channelQueryResponse.getChannelServiceFee());
                repaymentChannelConfig.setRate(channelQueryResponse.getChannelServiceFeeRate());
                repaymentChannelConfig.setPaymentChannelEnum(PaymentChannelEnum.getEnumByCode(channelQueryResponse.getChannelCode()));
                repaymentChannelConfig.setUserCodeWhiteList(channelQueryResponse.getUserCodeWhiteList());
                repaymentChannelConfigList.add(repaymentChannelConfig);
            }
        }
        LOGGER.info("=== getRepaymentChannelConfigList,response:{} ===", JSON.toJSONString(repaymentChannelConfigList));
        return repaymentChannelConfigList;
    }

    public RepaymentChannelConfig getChannelConfigById(int id, String bizNo) {
        List<RepaymentChannelConfig> repaymentChannelConfigList = getRepaymentChannelConfigList(bizNo);
        for (RepaymentChannelConfig repaymentChannelConfig : repaymentChannelConfigList) {
            if (repaymentChannelConfig.getId() == id) {
                return repaymentChannelConfig;
            }
        }
        throw new ApiErrorException("get channel error");
    }
}
